<template>
  <div class="products">
    <div class="page-header">
      <h1>商品管理</h1>
      <el-button type="primary" @click="$router.push('/products/add')">
        <el-icon><Plus /></el-icon>
        添加商品
      </el-button>
    </div>
    
    <!-- 搜索和筛选 -->
    <el-card class="search-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索商品名称或编号"
            prefix-icon="Search"
            clearable
          />
        </el-col>
        <el-col :span="6">
          <el-select v-model="category" placeholder="选择分类" clearable>
            <el-option label="电子产品" value="electronics" />
            <el-option label="服装" value="clothing" />
            <el-option label="食品" value="food" />
            <el-option label="家居" value="home" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-select v-model="status" placeholder="选择状态" clearable>
            <el-option label="上架" value="active" />
            <el-option label="下架" value="inactive" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-col>
      </el-row>
    </el-card>
    
    <!-- 商品列表 -->
    <el-card>
      <el-table
        :data="productList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="商品图片" width="100">
          <template #default="{ row }">
            <el-image
              :src="row.image"
              :preview-src-list="[row.image]"
              style="width: 60px; height: 60px"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" min-width="200" />
        <el-table-column prop="sku" label="商品编号" width="120" />
        <el-table-column prop="price" label="价格" width="100">
          <template #default="{ row }">
            ¥{{ row.price }}
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" width="80" />
        <el-table-column prop="category" label="分类" width="100" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'info'">
              {{ row.status === 'active' ? '上架' : '下架' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="$router.push(`/products/edit/${row.id}`)"
            >
              编辑
            </el-button>
            <el-button
              :type="row.status === 'active' ? 'warning' : 'success'"
              size="small"
              @click="toggleStatus(row)"
            >
              {{ row.status === 'active' ? '下架' : '上架' }}
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="deleteProduct(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      
      <!-- 批量操作 -->
      <div class="batch-actions" v-if="selectedProducts.length > 0">
        <el-button type="success" @click="batchUpdateStatus('active')">
          批量上架
        </el-button>
        <el-button type="warning" @click="batchUpdateStatus('inactive')">
          批量下架
        </el-button>
        <el-button type="danger" @click="batchDelete">
          批量删除
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 模拟数据
const searchKeyword = ref('')
const category = ref('')
const status = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
const selectedProducts = ref([])

const productList = ref([
  {
    id: 1,
    name: 'iPhone 15 Pro Max',
    sku: 'PHONE001',
    price: 9999,
    stock: 50,
    category: '电子产品',
    status: 'active',
    image: 'https://via.placeholder.com/60x60/409EFF/FFFFFF?text=iPhone'
  },
  {
    id: 2,
    name: 'Nike Air Max 270',
    sku: 'SHOE001',
    price: 899,
    stock: 120,
    category: '服装',
    status: 'active',
    image: 'https://via.placeholder.com/60x60/67C23A/FFFFFF?text=Nike'
  },
  {
    id: 3,
    name: '小米电视 65英寸',
    sku: 'TV001',
    price: 2999,
    stock: 25,
    category: '电子产品',
    status: 'inactive',
    image: 'https://via.placeholder.com/60x60/E6A23C/FFFFFF?text=小米'
  },
  {
    id: 4,
    name: '三只松鼠坚果礼盒',
    sku: 'FOOD001',
    price: 99,
    stock: 200,
    category: '食品',
    status: 'active',
    image: 'https://via.placeholder.com/60x60/F56C6C/FFFFFF?text=食品'
  }
])

const handleSearch = () => {
  // 搜索逻辑
  console.log('搜索:', { keyword: searchKeyword.value, category: category.value, status: status.value })
}

const resetSearch = () => {
  searchKeyword.value = ''
  category.value = ''
  status.value = ''
}

const handleSelectionChange = (val) => {
  selectedProducts.value = val
}

const toggleStatus = (row) => {
  row.status = row.status === 'active' ? 'inactive' : 'active'
  ElMessage.success(`${row.status === 'active' ? '上架' : '下架'}成功`)
}

const deleteProduct = (row) => {
  ElMessageBox.confirm(
    `确定要删除商品"${row.name}"吗？此操作不可恢复！`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    const index = productList.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      productList.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  })
}

const batchUpdateStatus = (newStatus) => {
  selectedProducts.value.forEach(product => {
    product.status = newStatus
  })
  ElMessage.success(`批量${newStatus === 'active' ? '上架' : '下架'}成功`)
}

const batchDelete = () => {
  ElMessageBox.confirm(
    `确定要删除选中的 ${selectedProducts.value.length} 个商品吗？此操作不可恢复！`,
    '批量删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    selectedProducts.value.forEach(product => {
      const index = productList.value.findIndex(item => item.id === product.id)
      if (index > -1) {
        productList.value.splice(index, 1)
      }
    })
    selectedProducts.value = []
    ElMessage.success('批量删除成功')
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.products {
  padding: 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  font-size: 24px;
  color: #333;
}

.search-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.batch-actions {
  margin-top: 20px;
  display: flex;
  gap: 10px;
}
</style>
